package com.by.dao.impl;

import com.by.dao.EmpDao;
import com.by.entity.Emp;
import com.by.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class EmpDaoImpl extends DBUtil implements EmpDao {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    @Override
    public List<Emp> getAllEmp() {
        List<Emp> list = new ArrayList<>();
        String sql = "select * from emp";
        conn = getConn();
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Emp emp = new Emp();
                emp.setEid(rs.getInt("eid"));
                emp.setEname(rs.getString("ename"));
                emp.setEsex(rs.getString("esex"));
                emp.setHiredate(rs.getString("hirdate"));
                emp.setSal(rs.getString("sal"));

                list.add(emp);

            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(conn, pstmt, rs);
        }


        return list;
    }
}
